package com.yztc.youbadbad.ui.other;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.yztc.youbadbad.R;
import com.yztc.youbadbad.helper.FragmentHelper;
import com.yztc.youbadbad.ui.other.fragment.GridcycleFragment;
import com.yztc.youbadbad.ui.other.fragment.HomepageFragment;
import com.yztc.youbadbad.ui.other.fragment.MineFragment;
import com.yztc.youbadbad.ui.other.fragment.OverseaFragment;
import com.yztc.youbadbad.ui.other.fragment.ShoppingcarFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.linearlayout_main)
    LinearLayout linearlayoutMain;
    @BindView(R.id.img_gridcircle_main)
    ImageView imgGridcircleMain;

    private List<Fragment> mFragmentList = new ArrayList<>();//fragment集合
    private FragmentManager mManager = getSupportFragmentManager();
    private long mNowtime;//两次退出时间


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initFragment();
        initView();

        int position = getIntent().getIntExtra("page", -1);
        if (position!=-1){
            FragmentHelper.replaceFragment(mManager, mFragmentList, position-1, R.id.framelayout_main, 0, 0);
            selectPositon(position);//选中购物车
        }

    }

    /**
     * 初始化视图以及底部导航点击事件
     */
    private void initView() {
        for (int i = 0; i < linearlayoutMain.getChildCount(); i++) {
            ImageView iv = (ImageView) linearlayoutMain.getChildAt(i);
            if (i == 0) {
                iv.setSelected(true);
            }
            iv.setTag(i);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = (int) view.getTag();
                    selectPositon(position);
                    FragmentHelper.switchFragment(mManager, mFragmentList, position, R.id.framelayout_main, 0, 0);
                   /* if (position== 3){
                        FragmentHelper.replaceFragment(mManager, mFragmentList, position, R.id.framelayout_main, 0, 0);
                    }*/
                }
            });
        }
    }

    /**
     * 底部导航选中变色
     * @param position
     */
    private void selectPositon(int position) {
        Log.e("tag", "position" + position);
        for (int i = 0; i < linearlayoutMain.getChildCount(); i++) {
            ImageView iv = (ImageView) linearlayoutMain.getChildAt(i);
            if (position == i) {
                if (position == 2) {
                    int color = getResources().getColor(R.color.colorAccent);
                    iv.setBackgroundColor(color);
                } else {
                    imgGridcircleMain.setBackgroundColor(Color.TRANSPARENT);
                }
                iv.setSelected(true);
            } else {
                iv.setSelected(false);
            }

        }
    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
        HomepageFragment homepageFragment = new HomepageFragment();
        mFragmentList.add(homepageFragment);
        OverseaFragment overseaFragment = new OverseaFragment();
        mFragmentList.add(overseaFragment);
        GridcycleFragment gridcycleFragment = new GridcycleFragment();
        mFragmentList.add(gridcycleFragment);
        ShoppingcarFragment shoppingcarFragment = new ShoppingcarFragment();
        mFragmentList.add(shoppingcarFragment);
        MineFragment mineFragment = new MineFragment();
        mFragmentList.add(mineFragment);
        mManager.beginTransaction().add(R.id.framelayout_main, homepageFragment).commit();
    }



    /**
     * 连续按两次返回则退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mNowtime > 2000) {
                Toast.makeText(this, "再按一次退出程序！", Toast.LENGTH_SHORT).show();
                mNowtime = System.currentTimeMillis();
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
